/**
 *
 */
package com.cwt_taxi_xa;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cwt_taxi_xa.model.AppInfo;

import java.util.List;

/**
 * @author zr
 * 自定义适配器类，提供给listView的自定义view
 */
public class SystemAppAdapter extends BaseAdapter {

	private static final String TAG = "SystemAppAdapter.";
	private List<AppInfo> mlistAppInfo = null;

	private LayoutInflater inflater = null;

	public SystemAppAdapter(Context context,  List<AppInfo> apps) {
		this.inflater = LayoutInflater.from(context);
		mlistAppInfo = apps ;
	}
	@Override
	public int getCount() {

		// TODO Auto-generated method stub
		//System.out.println("size" + mlistAppInfo.size());
		return mlistAppInfo.size();
	}
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mlistAppInfo.get(position);
	}
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	@Override
	public View getView(int position, View convertview, ViewGroup arg2) {
		try {
			ViewHolder holder = null;
			if (convertview == null) {
				convertview = inflater.inflate(R.layout.system_app_item, null);
				holder = new ViewHolder(convertview);
				convertview.setTag(holder);
			}else{
				holder = (ViewHolder) convertview.getTag() ;
			}
			AppInfo appInfo = (AppInfo) getItem(position);
			holder.appIcon.setImageDrawable(appInfo.getAppIcon());
			holder.tvAppLabel.setText(appInfo.getAppLabel());
			//holder.tvPkgName.setText(appInfo.getPkgName());
			return convertview;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	class ViewHolder {
		ImageView appIcon;
		TextView tvAppLabel;
		//TextView tvPkgName;

		public ViewHolder(View view) {
			this.appIcon = (ImageView) view.findViewById(R.id.ItemImage);
			this.tvAppLabel = (TextView) view.findViewById(R.id.ItemText);
			//this.tvPkgName = (TextView) view.findViewById(R.id.tvPkgName);
		}
	}
}
